<template>
  <BasicModal v-bind="$attrs" title="教育实习小组评价" @ok="handleSubmit" @register="registerModal">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref } from "vue";
  import { BasicForm, useForm } from "/@/components/Form";
  import { useModalInner } from "/@/components/Modal";
  import { useMessage } from "/@/hooks/web/useMessage";
  import BasicModal from "/@/components/Modal/src/BasicModal.vue";
  import { remarkFormSchema } from "/@/views/default/practical/practical.data";
  import { defaultModalFormSettings } from "/@/hooks/core/useDefault";
  import { setLectureNoteRemarkApi } from "/@/api/core/lectureNote";
  import { setLessonPlanRemarkApi } from "/@/api/core/lessonPlan";
  import { setTeachingStudyRemarkApi } from "/@/api/core/teachingStudy";
  import { setLeaderWorkRemarkApi } from "/@/api/core/leaderWork";
  import { PracTypeEnum } from "/@/enums/typeEnum";

  const params = ref<any>({});

  const [registerModal, { closeModal, changeLoading, changeOkLoading }] = useModalInner((data) => {
    params.value = data;
  });

  const [registerForm, { validate }] = useForm({
    schemas: remarkFormSchema,
    ...defaultModalFormSettings,
  });

  const { createMessage } = useMessage();

  async function handleSubmit() {
    try {
      const { groupEvaluation: value } = await validate();
      changeLoading(true);
      changeOkLoading(true);
      switch (params.value.target) {
        case PracTypeEnum.LectureNote: {
          await setLectureNoteRemarkApi(params.value.userId, params.value.noteId, value);
          createMessage.success("听课记录评价成功");
          break;
        }
        case PracTypeEnum.LessonPlan: {
          await setLessonPlanRemarkApi(params.value.userId, params.value.planId, value);
          createMessage.success("实习教案评价成功");
          break;
        }
        case PracTypeEnum.TeachingStudy: {
          await setTeachingStudyRemarkApi(params.value.userId, params.value.studyId, value);
          createMessage.success("教研活动评价成功");
          break;
        }
        case PracTypeEnum.LeaderWork: {
          await setLeaderWorkRemarkApi(params.value.userId, value);
          createMessage.success("班主任工作评价成功");
          break;
        }
      }
      closeModal();
    } finally {
      changeLoading(false);
      changeOkLoading(false);
    }
  }
</script>
